//
//  LBContrastFilter.mm
//  OpenGLGame
//
//  Created by Nguyen Cong Huy on 3/29/13.
//  Copyright (c) 2013 UIT. All rights reserved.
//

#include "LBContrastFilter.h"
#include "LBGraphics.h"

char fShaderStrContrast[] = ""
"precision mediump float;"

"varying vec2 vtexCoor;"
"varying vec4 vposition;"
"uniform sampler2D texture;"
"uniform float contrast;"


//contrast function (default 1.0)
"vec4 contrastColor(vec4 color, float contrast){"
"  color.r = (color.r - 0.5)*contrast + 0.5;"
"  color.g = (color.g - 0.5)*contrast + 0.5;"
"  color.b = (color.b - 0.5)*contrast + 0.5;"
"  color = clamp(color, 0.0, 1.0);"
"  return color;"
"}"
//END contrast function


"void main()"
"{"
"    lowp vec4 textureColor = texture2D(texture, vtexCoor);"
"    textureColor = contrastColor(textureColor, contrast);"
"    gl_FragColor = textureColor;"
"}";

LBContrastFilter::LBContrastFilter(){
    init(0, fShaderStrContrast);
    LBGraphics::getInstance()->useProgram(program);
    contrastUniform = program->uniformIndex("contrast");
    setContrast(1.0f);
}

void LBContrastFilter::setContrast(float contrast){
    this->contrast = contrast;
    LBGraphics::getInstance()->useProgram(program);
    glUniform1f(contrastUniform, contrast);
}